export default class Http {
	 // requestUrl = "https://api.ousukeji.com/"
	 requestUrl = "http://192.168.1.238:8080/"
	 // requestUrl = "http://192.168.0.156:8080/"
	 // requestUrl = "http://192.168.1.87:8080/"
	 // requestUrl = "http://192.168.1.27:8080/"


	async get(url) {
		let res = await this._request(url, "GET", {});
		console.log("网络请求", res);
		return res;
	}
async post(url,data) {
		let res = await this._request(url, "POST", data);
		
		console.log("网络请求", res);
		return res;
	}
	async _request(url, method, data) {
		let res = await this._promise(url, method, data)
		if (res.data.status&&res.data.status==500) {
			uni.removeStorageSync("token")
			let loginRes = await this._login()
			uni.setStorageSync("token", loginRes.data.result)
			let alginRes = await this._request(url, method, data)
			return alginRes;
		} else {

			return res
		}
	}

	async _promise(url, method, data) {
		var header = {
			"content-type": "application/json"
		};
		var token = uni.getStorageSync('token'); // 从本地缓存读取tokenName值
		if (token == undefined || !token) {
			let loginRes = await this._login()
			uni.setStorageSync("token", loginRes.data.result)
			header['satoken'] = loginRes.data.result;
		} else {
			header['satoken'] = token;
		}
		// 3、后续在发起请求时将 header 对象塞到请求头部
		return new Promise((resolve, reject) => {
			uni.request({
				url: this.requestUrl + url, // 仅为示例，并非真实接口地址。
				header: header,
				method: method,
				data: data,
				success: (res) => {
					console.log(res.data);
					resolve(res)
				},
				fail: fail => {
					reject(res)
				}
			})
		})

	}

	async _login() {
		
		let loginRes = await this._wxLogin();
		return new Promise((resolve, reject) => {
			uni.request({
				url: this.requestUrl+'v1/login/weixin', // 仅为示例，并非真实接口地址。
				header: {},
				method: 'POST',
				data: {
					code: loginRes.code
				},
				success: (res) => {
					console.log(res.data);
					resolve(res)
				},
				fail: fail => {
					reject(res)
				}
			})
		})
	}


	_wxLogin() {
		return new Promise((resolve, reject) => {
			uni.login({
				provider: 'weixin', //使用微信登录
				success: function(loginRes) {
					resolve(loginRes)
				}
			});
		})
	}
	
	temUpload(path) {
	
		return new Promise((resolve, reject) => {
			uni.showLoading({
				title: "正在上传中",
	
			})
			
		
			// 发起文件上传请求
			uni.uploadFile({
				url: this.requestUrl+"v1/upload/temporaryFile", // 后端API地址
				method: 'POST',
				header: {
					'Content-Type': 'application/json'
				},
				filePath: path,
				name: 'file',
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					console.error("文件上传失败", err);
					reject(err)
					// TODO: 错误处理
				},
				complete: (comp) => {
					uni.hideLoading()
				}
			});
		})
	}

uploadBaiduyun(path) {
	
		return new Promise((resolve, reject) => {
			uni.showLoading({
				title: "正在上传中",
	
			})
			
		
			// 发起文件上传请求
			uni.uploadFile({
				url: this.requestUrl+"v1/upload/uploadBaiduyun", // 后端API地址
				method: 'POST',
				header: {
					'Content-Type': 'application/json'
				},
				filePath: path,
				name: 'file',
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					console.error("文件上传失败", err);
					reject(err)
					// TODO: 错误处理
				},
				complete: (comp) => {
					uni.hideLoading()
				}
			});
		})
	}
	upload(path) {

		return new Promise((resolve, reject) => {
			uni.showLoading({
				title: "正在上传中",

			})
			
		
			// 发起文件上传请求
			uni.uploadFile({
				url: this.requestUrl+"v1/upload/file", // 后端API地址
				method: 'POST',
				header: {
					'Content-Type': 'application/json'
				},
				filePath: path,
				name: 'file',
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					console.error("文件上传失败", err);
					reject(err)
					// TODO: 错误处理
				},
				complete: (comp) => {
					uni.hideLoading()
				}
			});
		})
	}
}